﻿using GN.Pay.TradeChannel.DefaultImpl;
using GN.Pay.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace GN.Pay.TradeChannel.AliPay.Response
{
    /// <summary>
    /// 退款响应
    /// </summary>
    [Serializable]
    class RefundResponse
    {
        private readonly ITradeChannel channel;
        private readonly AliPayConfigure config;
        private readonly IRefundApplyInfo apply;

        internal RefundResponse(string responseString,
            ITradeChannel channel,
            AliPayConfigure config,
            IRefundApplyInfo apply)
        {
            if (responseString != null)
            {
                this.ResponseString = responseString.Trim();
            }
            else
            {
                this.ResponseString = responseString;
            }
            this.channel = channel;
            this.config = config;
            this.apply = apply;
            this.Init();
        }

        private static string Format_ErrorMessage = "响应结果格式不正确，是否已开通无密退款接口。";

        private void Init()
        {
            if (string.IsNullOrWhiteSpace(this.ResponseString))
            {
                this.channel.ThrowInterfaceException(Format_ErrorMessage);
            }
            IDictionary<string, object> xmlDic;
            try
            {
                xmlDic = XmlUtils.LoadXmlString(this.ResponseString, "alipay");
            }
            catch
            {
                throw this.channel.ThrowInterfaceException(Format_ErrorMessage);
            }
            object is_success;
            if (!xmlDic.TryGetValue("is_success", out is_success) || is_success == null)
            {
                throw this.channel.ThrowInterfaceException(Format_ErrorMessage);
            }
            if (is_success.ToString().Equals("T", StringComparison.InvariantCultureIgnoreCase))
            {
                this.IsSuccess = true;
            }
            else
            {
                object errorCode;
                this.IsSuccess = false;
                if (xmlDic.TryGetValue("error", out errorCode) || errorCode != null)
                {
                    this.ErrorCode = errorCode.ToString();
                }
                else
                {
                    this.ErrorCode = "";
                }
            }
        }

        /// <summary>
        /// 获取响应字符
        /// </summary>
        public string ResponseString { get; private set; }

        /// <summary>
        /// 获取是否成功
        /// </summary>
        public bool IsSuccess { get; private set; }

        /// <summary>
        /// 获取错误代码
        /// </summary>
        public string ErrorCode { get; private set; }

        /// <summary>
        /// 转为结果
        /// </summary>      
        /// <returns></returns>
        public ITradeRefundApplyResult ToResult()
        {
            var state = TradeApplyResultState.Apply_Success_Wiat_Notify;
            var msg = "";
            if (!this.IsSuccess)
            {
                state = TradeApplyResultState.Apply_Error;
                msg = this.ErrorCode;
            }
            return new TradeRefundApplyResult(
                this.channel,
                apply.RefundNo,
                apply.TradeNo,
                apply.ApplyMoney,
                0m, null,
                state,
                msg);
        }
    }
}
